<template>
  <div>
    <div class="search-list mb-10">
      <el-button type="primary" @click="editCate('add', null)" size="small" plain><i
          class="el-icon-plus"></i>添加晨检项
      </el-button>
    </div>
    <div class="table-line-body mt-10 yangTable">
      <el-table
          :data="treeList"
          row-key="id"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="name" label="晨检项" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="editCate('edit', scope.row)"
            >修改
            </el-button>
            <el-button
                v-if="!scope.row.parent_id"
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="editCate('add', scope.row)"
            >新增
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="deletefn(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="height40"></div>
    <detail :visible.sync="patrolVisible" :optionData="optionData" @InitFn="InitFn"
            :selectList="selectList" :optionType="optionType"></detail>
  </div>
</template>

<script>
import detail from "./detail";
import {MorningCheckIndex, MorningCheckDelete} from "@/api/admin/ClassDynamics"

export default {
  components: {
    detail
  },
  data() {
    return {
      tableStripe: true, // 是否斑马栏
      tableBorder: true, // 单元格右边边框
      arryList: [], // 数据列表
      patrolVisible: false,
      // 搜索
      searchForm: {},
      checkTab: '',
      optionData: null,
      optionType: '',
      treeList: [],
      isExpandAll: false,
      selectList: []
    }
  },
  watch: {
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    async InitFn() {
      MorningCheckIndex({p:0}).then(res => {
        console.log('res:', res)
        if (!res.code) {
          this.treeList = this.$handleTree(res.list, 'id', 'parent_id');
          let list = this.$handleTree(res.list, 'id', 'parent_id')
          list.unshift({id: 0, name: '主类目'})
          console.log('list:', list)
          this.selectList = list
        }
      })
    },
    handleSelectionChange(val) {
      this.handleSelection = val
    },
    deletefn(row) {
      this.$confirm(`确认删除【${row.name}】, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(row.id)
      }).catch(() => {
      })
    },
    async detelRespon(id) {
      MorningCheckDelete({id:id}).then(res=>{
        if (!res.code) {
          this.$message.success(res.msg)
          this.InitFn()
        }
      })
    },
    editCate(type, row) {
      this.optionType = type
      this.optionData = row
      this.patrolVisible = true
    }
  },
  mounted() {
  },
}
</script>
<style>
.lead-list {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fddeae;
  margin-bottom: 5px;
  border-radius: 2px;
  padding: 5px 0;
}

.lead-list:last-child {
  margin-bottom: 0;
}

.lead-list span {
  margin-right: 30px;
}
</style>

